package com.cloud.iotdb;

import org.fusesource.mqtt.client.BlockingConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class EmmitToIotdb {

   public static void main(String[] args) {
       String[] hardwares = new String[]{
               "a1",
               "b1",
               "b2",
               "c3",
               "d1",
               "f5"
      };
       int count = 1000;

       for(int i = 0; i < count ;i++){
           IotdbVO iotdbVO = new IotdbVO();
           iotdbVO.setDevice("root.test.device.wg");
           List<String> measurements = new ArrayList<>();
           List<Object> values = new ArrayList<>();
           measurements.add("temperature");
           measurements.add("status");
           measurements.add("hardware");

           Random r = new Random();
           values.add(r.nextInt(40));
           values.add(r.nextBoolean());
           values.add(hardwares[r.nextInt(hardwares.length)]);

           iotdbVO.setMeasurements(measurements);
           iotdbVO.setValues(values);
           emmitToIotdb(iotdbVO);
      }
  }

   public static void emmitToIotdb(IotdbVO content){
       try {
           MQTT mqtt = new MQTT();
           mqtt.setHost("47.105.144.190", 1883);
           mqtt.setUserName("root");
           mqtt.setPassword("root");

           BlockingConnection connection = mqtt.blockingConnection();
           connection.connect();

           String payload = content.toJson();
           connection.publish(content.getDevice(),payload.getBytes(), QoS.AT_LEAST_ONCE,false);

           connection.disconnect();
      } catch (Exception e){
           e.printStackTrace();
      }

  }


}
